<?php
/**
 * Created by PhpStorm.
 * User: xionghao
 * Date: 2017/10/17
 * Time: 14:16
 */

namespace app\meeting\controller;


use app\meeting\model\MeetArticleModel;
use app\meeting\model\MeetCategoryModel;
use app\meeting\model\MeetModel;
use cmf\controller\AdminBaseController;
use think\Db;

class AdminIndexController extends AdminBaseController
{
    public function index()
    {
        $name = $this->request->param('name');
        $start_time = $this->request->param('start_time');
        $end_time = $this->request->param('end_time');
        $where = [];
        if ($name) {
            $where['name'] = ['like', "%$name%"];
        }
        if ($start_time) {
            $where['start_time'] = ['> time', $start_time];
        }
        if ($end_time) {
            $where['end_time'] = ['<= time', $end_time];
        }
        $meetingModel = new MeetModel();
        $list = $meetingModel->with('province_text')->where($where)->paginate();
        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return $this->fetch("index");
    }

    public function add()
    {
        $provinces = Db::name('provinces')->select();
        $this->assign('provinces', $provinces);
        $categories = MeetCategoryModel::all();
        $this->assign('categories', $categories);
        return $this->fetch();
    }

    public function edit($id){
        $provinces = Db::name('provinces')->select();
        $this->assign('provinces', $provinces);
        $categories = MeetCategoryModel::all();
        $this->assign('categories', $categories);
       $meeting = MeetModel::get($id);

       $this->assign('meet',$meeting);

       return $this->fetch();
    }

    public function addPost()
    {
        $data = $this->request->param();
        $result_error = $this->validate($data, 'meet');
        if ($result_error !== true) {
            $this->error($result_error);
        } else {
            $meetModel = new MeetModel();
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['signed_time'] = strtotime($data['signed_time']);
            $data['register_end_time'] = strtotime($data['register_end_time']);

            $result = $meetModel->allowField(true)->save($data);
            if ($result) {
                $province_id = $data['province'];
                $province = Db::name('provinces')->where('provinceid', $province_id)->find();

                $tag = Db::name('meet_tags')->where('pid', $province['provinceid'])->find();
                if (!$tag) {
                    Db::name('meet_tags')->insert(['name' => $province['province'], 'pid' => $province['provinceid']]);
                }
                $this->success('添加成功', 'index');
            }else {
                $this->error('添加失败');
            }

        }
    }

    public function articles($id){
        $articleModel = new MeetArticleModel();
       $articles = $articleModel->where('mid',$id)->select();
       $this->assign('list',$articles);
       $this->assign('mid',$id);
       return $this->fetch();
    }

    public function editArticlePost($id){
        $post = $this->request->param('post/a');
        $articleModel =  MeetArticleModel::get($id);
        $result = $articleModel->allowField(true)->save($post);
        if($result){
            $this->success('编辑成功');
        }else {
            $this->error('编辑失败');
        }
    }

    public function deleteArticle($id){
      $result = MeetArticleModel::destroy($id);
        if($result){
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }

    public function editArticle($id){
        $this->assign('mid',$id);
        $result = MeetArticleModel::get($id);
        $this->assign('article',$result);
        return $this->fetch();
    }

    public function addArticlePost($mid){
       $post = $this->request->param('post/a');
       $articleModel = new MeetArticleModel();
       $post['mid'] = $mid;
       $result = $articleModel->allowField(true)->save($post);
       if($result){
           $this->success('添加成功');
       }else {
           $this->error('添加失败');
       }
    }

    public function addArticle($id){
        $this->assign('mid',$id);
        return $this->fetch();
    }


    public function delete($id){
        $meetModel = MeetModel::get($id);
        $articleModel = new MeetArticleModel();
       $count = $articleModel->where('mid',$id)->count();
       if($count > 0){
           $this->error('该会议下还有投稿，请删除后继续');
       }else {
           $meetModel->delete();
           $this->success('删除成功');
       }
    }

    public function editPost($id)
    {
        $data = $this->request->param();
        $result_error = $this->validate($data, 'meet');
        if ($result_error !== true) {
            $this->error($result_error);
        } else {
            $meetModel = MeetModel::get($id);
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['signed_time'] = strtotime($data['signed_time']);
            $data['register_end_time'] = strtotime($data['register_end_time']);
            if(empty($data['tg_status'])){
                $data['tg_status'] = 0;
            }

            if(empty($data['tg_status'])){
                $data['tg_status'] = 0;
            }

            if(empty($data['person_status'])){
                $data['person_status'] = 0;
            }
            if(empty($data['unit_status'])){
                $data['unit_status'] = 0;
            }
            $result = $meetModel->allowField(true)->save($data);
            if ($result) {
                $province_id = $data['province'];
                $province = Db::name('provinces')->where('provinceid', $province_id)->find();

                $tag = Db::name('meet_tags')->where('pid', $province['provinceid'])->find();
                if (!$tag) {
                    Db::name('meet_tags')->insert(['name' => $province['province'], 'pid' => $province['provinceid']]);
                }
                $this->success('修改成功', 'index');
            }else {
                $this->error('修改失败');
            }

        }
    }

}